package com.zhuchaooasystem.presenter.setting;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.socks.library.KLog;
import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.ChangePWEntity;
import com.zhuchaooasystem.entity.UserEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.ui.home.HomeActivity;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.EncryptUtils;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.OkDialog;

import java.util.HashMap;
import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by zhangyajiang on 2016/12/22.
 */

public class ChangePasswordPresenter extends BasePresenter<ChangePasswordContract.View> implements ChangePasswordContract.Presenter {

    public ChangePasswordPresenter(Activity activity, ChangePasswordContract.View view) {
        super(activity, view);
    }

    @Override
    public void changePassword() {
        String pw_old = mView.getOldPassword();
        final String new_password = mView.getnewPassword();
        String new_pw_again = mView.getnewPasswordAgain();
        //----
        if (TextUtils.isEmpty(pw_old)) {
            handleError("请输入旧密码");
            return;
        }
        if (!(!TextUtils.isEmpty(new_password) && new_password.length() >= 6)) {
            handleError("密码不得低于6位");
            return;
        }
        if (!(new_password.equals(new_pw_again))) {
            handleError("两次输入密码不一致");
            return;
        }
        showLoading();
        mView.setLoginClick(false);

        //----
        Map<String, String> map = new HashMap<>();
        map.put("userName", UserInfo.getUName());
        map.put("oldPassword", pw_old);
        map.put("newPassword", new_password);
        //----
        Subscription subscription = HttpFactory.getHttpApi().updateUserPassword(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {

                        LoadingDiaogDismiss();
                        mView.setLoginClick(true);

                        if (null != o) {
                            ChangePWEntity entity = (ChangePWEntity) o;

                            //新密码需要保存到数据库里面
                            mView.getResult(entity,new_password);
                        } else {
                            handleError("修改失败");
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LoadingDiaogDismiss();
                        mView.setLoginClick(true);
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }
}
